package com.southwind.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.southwind.entity.ProductCategory;
import com.southwind.entity.ProductInfo;
import com.southwind.mapper.ProductCategoryMapper;
import com.southwind.mapper.ProductInfoMapper;
import com.southwind.service.IProductCategoryService;
import com.southwind.vo.ProductCategoryVO;
import com.southwind.vo.ProductInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 类目表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2021-04-18
 */
@Service
public class ProductCategoryServiceImpl extends ServiceImpl<ProductCategoryMapper, ProductCategory> implements IProductCategoryService {

    @Autowired
    private ProductCategoryMapper productCategoryMapper;

    @Autowired
    private ProductInfoMapper productInfoMapper;

    /**
     * 查询所有商品
     * @return List<ProductCategoryVO>
     */
    @Override
    public List<ProductCategoryVO> productCategoryVOList() {
        ArrayList<ProductCategoryVO> productCategoryVOList = new ArrayList<>();
        // 1.查出类目
        List<ProductCategory> productCategoryList = productCategoryMapper.selectList(null);
        for (ProductCategory productCategory : productCategoryList) {
            // 2.查出商品
            QueryWrapper<ProductInfo> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("category_type", productCategory.getCategoryType());
            List<ProductInfo> productInfoList = productInfoMapper.selectList(queryWrapper);
            ArrayList<ProductInfoVO> ProductInfoVOList = new ArrayList<>();
            for (ProductInfo productInfo : productInfoList) {
                // 封装ProductInfoVO
                ProductInfoVO productInfoVO = new ProductInfoVO();
                productInfoVO.setProductId(productInfo.getProductId());
                productInfoVO.setProductName(productInfo.getProductName());
                productInfoVO.setProductPrice(productInfo.getProductPrice());
                productInfoVO.setProductDescription(productInfo.getProductDescription());
                productInfoVO.setProductIcon(productInfo.getProductIcon());
                productInfoVO.setProductStock(productInfo.getProductStock());
                ProductInfoVOList.add(productInfoVO);
            }
            // 封装productCategoryVO
            ProductCategoryVO productCategoryVO = new ProductCategoryVO();
            productCategoryVO.setName(productCategory.getCategoryName());
            productCategoryVO.setType(productCategory.getCategoryType());
            productCategoryVO.setFoods(ProductInfoVOList);
            productCategoryVOList.add(productCategoryVO);
        }
        return productCategoryVOList;
    }

    @Override
    public List<ProductCategoryVO> onlyProductCategoryVOList() {
        ArrayList<ProductCategoryVO> productCategoryVOList = new ArrayList<>();
        // 1.查出类目
        List<ProductCategory> productCategoryList = productCategoryMapper.selectList(null);
        for (ProductCategory productCategory : productCategoryList) {
            // 封装productCategoryVO
            ProductCategoryVO productCategoryVO = new ProductCategoryVO();
            productCategoryVO.setName(productCategory.getCategoryName());
            productCategoryVO.setType(productCategory.getCategoryType());
            productCategoryVOList.add(productCategoryVO);
        }
        return productCategoryVOList;
    }
}
